home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MoreKittiesPlease / Source / FragUtils.c < prev    next >
Text File  |  2000-06-23  |  3KB  |  106 lines

  1. /*
  2.     File:        FragUtils.c
  3.  
  4.     Contains:    CFM library access to resources.
  5.  
  6.     Written by:    Quinn "The Eskimo!"
  7.  
  8.     Copyright:    © 1998 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.     You may incorporate this sample code into your applications without
  13.     restriction, though the sample code has been provided "AS IS" and the
  14.     responsibility for its operation is 100% yours.  However, what you are
  15.     not permitted to do is to redistribute the source as "DSC Sample Code"
  16.     after having made changes. If you're going to re-distribute the source,
  17.     we require that you make it clear in the source that the code was
  18.     descended from Apple Sample Code, but that you've made changes.
  19. */
  20.  
  21. /////////////////////////////////////////////////////////////////////
  22.  
  23.  
  24. /////////////////////////////////////////////////////////////////////
  25. // OT Stuff
  26.  
  27. #import <OTDebug.h>
  28.  
  29. /////////////////////////////////////////////////////////////////////
  30. // Our prototypes
  31.  
  32. #import "FragUtils.h"
  33.  
  34. /////////////////////////////////////////////////////////////////////
  35. // The following has disappeared from Universal Interfaces 3.1
  36. // but it is so wonderfully useful.
  37.  
  38. #if UNIVERSAL_INTERFACES_VERSION >= 0x0300
  39.  
  40.     #define IsFileLocation(where)     \
  41.         ( ((where) == kDataForkCFragLocator)    ||  \
  42.         ((where) == kResourceCFragLocator) )
  43.  
  44. #endif
  45.  
  46. /////////////////////////////////////////////////////////////////////
  47.  
  48. static AliasHandle gAliasToLibraryFile = nil;
  49.  
  50. // ••• need to explicitly preserve CurResFile in this and all its clients
  51.  
  52. extern OSStatus FUOpenResourceFork(SInt16 *refNum)
  53. {
  54.     OSStatus err;
  55.     Boolean junkWasChanged;
  56.     FSSpec fss;
  57.     
  58.     *refNum = 0;
  59.     err = noErr;
  60.     if (gAliasToLibraryFile == nil) {
  61.         err = fnfErr;
  62.     }
  63.     if (err == noErr) {
  64.         err = ResolveAlias(nil, gAliasToLibraryFile, &fss, &junkWasChanged);
  65.     }
  66.     if (err == noErr) {
  67.  
  68.         // By opening the resource file read-only, we ensure
  69.         // that we *alway* get our own copy of the resource map,
  70.         // so we can close it safely when we're done.  See
  71.         // DTS Technote 1••• "OpenResFile Twice Considered Hard" for
  72.         // details.
  73.         
  74.         *refNum = FSpOpenResFile(&fss, fsRdPerm);
  75.         err = ResError();
  76.         OTAssert("FUOpenResourceFork: FSpOpenResFile misbehaving", err != noErr || *refNum != 0);
  77.     }
  78.  
  79.     return err;
  80. }
  81.  
  82. extern OSStatus FUInitialise(const CFragInitBlock *theInitBlock)
  83. {
  84.     OSStatus err;
  85.  
  86.     // OTAssert("FUInitialise: Not in system heap!", GetZone() == SystemZone());
  87.         
  88.     err = noErr;
  89.     if ( IsFileLocation(theInitBlock->fragLocator.where) ) {
  90.         err = NewAlias(nil, theInitBlock->fragLocator.u.onDisk.fileSpec, &gAliasToLibraryFile);
  91.         OTAssert("FUInitialise: NewAlias misbehaving", err != noErr || gAliasToLibraryFile != nil);
  92.     }
  93.     OTAssert("FUInitialise: Error creating alias to library file", err == noErr);
  94.     
  95.     return err;
  96. }
  97.  
  98. extern void FUTerminate(void)
  99. {
  100.     if (gAliasToLibraryFile != nil) {
  101.         DisposeHandle( (Handle) gAliasToLibraryFile);
  102.         OTAssert("FUTerminate: Error disposing alias", MemError() == noErr);
  103.         gAliasToLibraryFile = nil;
  104.     }
  105. }
  106.